import requests
import time

# 测试API连接的简单脚本
def test_api_connection():
    url = "http://localhost:8000"
    print(f"尝试连接到API服务: {url}")
    
    try:
        # 发送GET请求到根端点
        response = requests.get(url, timeout=10)
        print(f"✓ 连接成功！状态码: {response.status_code}")
        print(f"响应内容: {response.text[:200]}...")
        
        # 尝试访问API文档端点
        docs_url = f"{url}/docs"
        docs_response = requests.get(docs_url, timeout=10)
        print(f"\n尝试访问API文档: {docs_url}")
        print(f"✓ API文档访问成功！状态码: {docs_response.status_code}")
        
        # 尝试访问测试用户管理服务的端点
        execute_url = f"{url}/api/services/execute"
        print(f"\n尝试访问用户管理服务端点: {execute_url}")
        
        # 构造一个简单的测试请求
        test_payload = {
            "query": "查询用户admin的信息",
            "user_id": "admin"
        }
        
        execute_response = requests.post(
            execute_url,
            json=test_payload,
            timeout=10
        )
        
        print(f"✓ 用户管理服务访问成功！状态码: {execute_response.status_code}")
        print(f"响应内容: {execute_response.text[:500]}...")
        
    except requests.exceptions.ConnectionError as e:
        print(f"❌ 连接失败: {e}")
    except requests.exceptions.Timeout as e:
        print(f"❌ 请求超时: {e}")
    except Exception as e:
        print(f"❌ 发生错误: {e}")

if __name__ == "__main__":
    print("=== API连接测试脚本 ===")
    print("等待3秒让服务完全就绪...")
    time.sleep(3)
    test_api_connection()